package com.qkjia.zqkspringshell;


import org.jline.reader.Parser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.context.annotation.Lazy;
import org.springframework.core.annotation.Order;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.shell.Shell;
import org.springframework.shell.jline.InteractiveShellApplicationRunner;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.PriorityQueue;
import java.util.Queue;
import java.util.stream.Collectors;


@SpringBootApplication(exclude = DataSourceAutoConfiguration.class,
scanBasePackages = "com.qkjia.zqkspringshell")
public class ZqkspringshellApplication {

    public static void main(String[] args) {
        SpringApplication.run(ZqkspringshellApplication.class, args);
    }

    //打包
    //    mvn clean && mvn package -DskipTests

/*   修改禁用内置
    @SpringBootApplication
    public class SpringShellApplication {

        public static void main(String[] args) {

            // 指定禁用内置命令列表
            String[] disableCmds = new String[]{
                    "--spring.shell.command.clear.enabled=false",
                    "--spring.shell.command.stacktrace.enabled=false"
            };

            // 重新拼接启动参数
            String[] fullArgs = StringUtils.concatenateStringArrays(args, disableCmds);

            // 启动应用
            SpringApplication.run(SpringShellApplication.class, fullArgs);
        }
    }*/

    /*
        2.3 自定义ApplicationRuuner
        自定义实现ApplicationRunner 接口的实现类
        将自定义ApplicationRunner 注册为Spring的一个组件, 即使用@Component修饰
设置自定义Runner运行顺序, 保证运行顺序为: ScriptShellApplicationRunner > 自定义Runner > InteractiveShellApplicationRunner, 使用@Order 限制. 只有这样能让脚本参数和命令参数共存.
        延迟注入内置属性: Parser, Shell, enviroment*/
    /**
     * @Description: 自定义命令Runner, 启动应用后直接执行多条命令, 执行后结束
     * @author: zongf
     * @date: 2019-01-28 09:58
     */
/*    @Component
    @Order(InteractiveShellApplicationRunner.PRECEDENCE - 50)  // order 越小, 越先执行
    public class MyCommandsRunner implements ApplicationRunner {

        @Lazy
        @Autowired
        private Parser parser;

        @Lazy
        @Autowired
        private Shell shell;

        @Lazy
        @Autowired
        private ConfigurableEnvironment environment;

        @Override
        public void run(ApplicationArguments args) throws Exception {
            //过滤掉所有@开头的参数, @开头的参数交给ScriptShellApplicationRunner 处理
            List<String> cmds = args.getNonOptionArgs().stream()
                    .filter(s -> !s.startsWith("@"))
                    .collect(Collectors.toList());

            //如果启动参数中, 命令不为空, 则执行
            if (cmds != null && cmds.size() > 0) {
                // 关闭交互式应用: 确保关闭应用后, 直接退出应用, 不停留在交互式窗口中
                InteractiveShellApplicationRunner.disable(environment);

                // 将所有命令存储到队列中
                Queue<String> queue = new PriorityQueue<>();
                queue.addAll(cmds);

                // 执行命令
                shell.run(new MyInputProvider(parser, queue));
            }
        }*/
}
